package gui;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.table.TableModel;

import util.Settings;
import core.LocalMusicLibrary;
import core.Song;

public class AuroraGUI extends AuroraGUI_Beans {

	private JFileChooser jfc;
	protected SongTable queueTableModel;
	protected SongTable libraryTableModel;

	public AuroraGUI() {
		final Settings settings = Settings.get();
		setLocation(settings.getSettingAsPoint(Settings.FRAME_LOCATION));
		setSize(settings.getSettingAsDimension(Settings.FRAME_SIZE));
		libraryTable.addMouseListener(getMouseListener());
		queueTable.addKeyListener(getKeyListener());
		/*
		 * queueTable.getSelectionModel().addListSelectionListener( new
		 * ListSelectionListener() {
		 * 
		 * @Override public void valueChanged(ListSelectionEvent e) {
		 * System.out.println("here"); libraryTable.requestFocusInWindow(); }
		 * });
		 */
		loadLibrarySongs(new File(settings.getSetting(Settings.LOCAL_LIBRARY)));
	}

	public SongTable getQueueTable() {
		return queueTableModel;
	}

	private JFileChooser getFileChooser() {
		if (jfc == null) {
			jfc = new JFileChooser();
		}
		return jfc;
	}

	private void loadLibrarySongs(final File libraryFolder) {
		final LocalMusicLibrary lib = new LocalMusicLibrary();
		libraryTableModel.setSource(lib);
		new Thread(new Runnable() {
			public void run() {
				lib.load(libraryFolder);
				System.out.println("Loaded " + lib.getNumSongs() + " songs.");
			}
		}).start();
	}

	@Override
	public void changeLibraryLocation() {
		jfc = getFileChooser();
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		File oldLibraryLoc = new File(Settings.get().getSetting(
				Settings.LOCAL_LIBRARY));
		jfc.setSelectedFile(oldLibraryLoc);
		int result = jfc.showDialog(this, "Set Library");
		if (result == JFileChooser.APPROVE_OPTION) {
			File selected = jfc.getSelectedFile();
			if (selected.isDirectory()) {
				Settings.get().putSetting(Settings.LOCAL_LIBRARY,
						selected.getPath());
				loadLibrarySongs(selected);
			}
		}
	}

	@Override
	protected TableModel createLibraryTableModel() {
		libraryTableModel = new SongTable();
		return libraryTableModel;
	}

	@Override
	protected TableModel createQueueTableModel() {
		queueTableModel = new SongTable();
		return queueTableModel;
	}

	@Override
	public void exit() {
		try {
			Settings settings = Settings.get();
			settings.putSetting(Settings.FRAME_LOCATION, getX() + " " + getY());
			settings.putSetting(Settings.FRAME_SIZE, getWidth() + " "
					+ getHeight());
			settings.saveSettings();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.exit(0);
	}

	private MouseAdapter getMouseListener() {
		return new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				int count = e.getClickCount();
				if (count == 2) {
					int row = libraryTable.getSelectedRow();
					Song song = libraryTableModel.getLibrary().getSongAt(row);
					if (song != null) {
						queueTableModel.getLibrary().addSong(song);
					}
				}
			}

		};
	}

	private KeyAdapter getKeyListener() {
		return new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				int code = e.getKeyCode();
				if (code == KeyEvent.VK_DELETE) {
					int row = queueTable.getSelectedRow();
					queueTableModel.getLibrary().removeSongAt(row);
				}
			}

		};
	}
}
